#include "gameselect.h"
#include "gamehall.h"
#include "gameroom.h"

#include <QTextEdit>
#include <QFile>
#include <QTextStream>
#include <QTextCodec>
#include <QDebug>

GameSelect::GameSelect(QWidget *parent) : QWidget(parent)
{
    this->setFixedSize(1000, 800);
    this->setWindowTitle("游戏关卡选择");
    this->setWindowIcon(QIcon(":/res/ico.png"));

    QPushButton* back(new QPushButton(this));
    back->setIcon(QIcon(":/res/back.png"));
    back->setFixedSize(30, 30);
    back->move(this->width() - 60, this->height() - 60);
    back->setCursor(QCursor((Qt::CursorShape::PointingHandCursor)));
    connect(back, &QPushButton::clicked, this, [=](){
        QSound::play(":/res/clicked.wav");
        GameHall* gamehall = new GameHall;
        gamehall->setFixedSize(this->width(), this->height());
        this->close();
        gamehall->show();
    });

    QVBoxLayout* VLayout(new QVBoxLayout(this));
    VLayout->setContentsMargins(380, 200, 380, 200);
    this->setLayout(VLayout);
    GameRoom* gameroom = new GameRoom;
    QPushButton* easyMode(new QPushButton(this));
    setButtonAttribute(easyMode, "简单模式");
    connect(easyMode, &QPushButton::clicked, this, [=](){
        QSound::play(":/res/clicked.wav");
        gameroom->setGeometry(this->geometry());
        gameroom->setMoveTimeout(300);
        this->close();
        gameroom->show();
    });

    QPushButton* normalMode(new QPushButton(this));
    setButtonAttribute(normalMode, "正常模式");
    connect(normalMode, &QPushButton::clicked, this, [=](){
        QSound::play(":/res/clicked.wav");
        gameroom->setGeometry(this->geometry());
        gameroom->setMoveTimeout(200);
        this->close();
        gameroom->show();
    });

    QPushButton* hardMode(new QPushButton(this));
    setButtonAttribute(hardMode, "困难模式");
    connect(hardMode, &QPushButton::clicked, this, [=](){
        QSound::play(":/res/clicked.wav");
        gameroom->setGeometry(this->geometry());
        gameroom->setMoveTimeout(100);
        this->close();
        gameroom->show();
    });

    QPushButton* historyScore(new QPushButton(this));
    setButtonAttribute(historyScore, "历史战绩");
    connect(historyScore, &QPushButton::clicked, this, [=](){
        QWidget* widget = new QWidget;
        widget->setWindowTitle("历史战绩");
        widget->setWindowIcon(QIcon(":/res/ico.png"));
        widget->setFixedSize(500, 300);
        QTextEdit* text = new QTextEdit(widget);
        text->setFont(QFont("华文行楷", 15));
        text->setFixedSize(500, 300);
        QFile file("D:/QTcode/snake/score.txt");
        if (!file.open(QIODevice::ReadOnly))
            qDebug() << "文件打开失败";
        QTextStream in(&file);
        text->append("历史最高得分: " + in.readLine());
        widget->show();
        file.close();
    });
    VLayout->addWidget(easyMode);
    VLayout->addWidget(normalMode);
    VLayout->addWidget(hardMode);
    VLayout->addWidget(historyScore);
}

void GameSelect::paintEvent(QPaintEvent *event)
{
    (void)event;
    QPainter painter(this);
    QPixmap pixmap(":/res/game_select.png");
    painter.drawPixmap(0, 0, this->width(), this->height(), pixmap);
}

void GameSelect::setButtonAttribute(QPushButton* button, const QString& text)
{
    button->setText(text);
    button->setFont(QFont("华文行楷", 15));
    button->setStyleSheet("background: white; padding: 8px 15px; border: 1px solid white; border-radius: 15px");
    button->setCursor(QCursor((Qt::CursorShape::PointingHandCursor)));
}
